home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_01_07 / 1n07055a < prev    next >
Text File  |  1990-11-05  |  4KB  |  148 lines

  1.  
  2.  
  3. /*******************************************************
  4. * DXRECV.C -- Receive a file from another network node.
  5. *
  6. *     Copyright 1990 Tom Jensen
  7. *******************************************************/
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <conio.h>
  12. #include <string.h>
  13. #include "netbios.h"
  14. #include "dxdef.h"
  15.  
  16. #define ESC 0x1b
  17.  
  18. int IFLAG;
  19.  
  20. void DispText(char *filename);
  21. static void DispHelp(void);
  22.  
  23. int main(int argc, char **argv)
  24. {
  25.   struct NcbData  *ncb;
  26.   char            buffer [BUFSIZE];
  27.   char            lname[17], fname[17];
  28.   unsigned        retcode;
  29.   unsigned char   session = 0;
  30.  
  31.   if(retcode = NetActive())
  32.     exit(retcode);
  33.  
  34.   if((ncb =
  35.     (struct NcbData *) malloc(sizeof(struct NcbData)))
  36.     == NULL)
  37.     exit(1);
  38.  
  39.   if(argc < 2)
  40.   {
  41.     DispHelp();
  42.     exit(1);
  43.   }
  44.  
  45.   /* process recipient's name */
  46.   NbExpandName(fname, argv[1]);
  47.   fname[15] = NAME_UNIQUE;    /* make the name unique */
  48.   fname[16] = '\0';           /* make it a C string */
  49.  
  50.   /* process local (recipient's) name */
  51.   LocalName(buffer);          /* Get local net name */
  52.   NbExpandName(lname, buffer);
  53.   lname[15] = NAME_UNIQUE;    /* make the name unique */
  54.   lname[16] = '\0';           /* make it a C string */
  55.  
  56.   printf("Wait for unique name to be added.\n");
  57.   if((retcode = NbAddName(ncb, lname)) == NB_OK)
  58.     printf("Name \"%s\" added successfully.\n", lname);
  59.   else
  60.   {
  61.     printf("NETBIOS error %02xH encountered ", retcode);
  62.     printf(" encountered while adding \"%s\".\n", lname);
  63.     exit(1);
  64.   }
  65.  
  66.   if((retcode = NbPostListen(ncb, lname, fname))
  67.     == NB_OK)
  68.   {
  69.     printf("Waiting for Call from \"%s\" ...\n", fname);
  70.     printf("Press the ESC key to abort.\n");
  71.     while (! IFLAG)
  72.     {
  73.       if(kbhit())
  74.         if(getch() == ESC)
  75.         {
  76.           printf("Program aborted!\n");
  77.           NbDelName(ncb, lname);
  78.           exit(2);
  79.         }
  80.     }
  81.     if((session = ncb->LSN) > 0)
  82.       printf("Session opened successfully.\n");
  83.   }
  84.  
  85.   if (session == 0)
  86.   {
  87.     printf("NETBIOS error %02xH ", retcode);
  88.     printf("returned from Listen command.\n");
  89.     NbCloseSession(ncb, lname, fname, session);
  90.     NbDelName(ncb, lname);
  91.     exit(1);
  92.   }
  93.  
  94.   *buffer = '\0';
  95.  
  96.   NbReceiveData(ncb, lname, fname, buffer, BUFSIZE,
  97.     session);
  98.  
  99.   printf("Message received:\n    %s\n", buffer);
  100.  
  101.   if(strncmp(buffer, DXPREFIX, DXPFSIZE) == 0)
  102.   {
  103.     NbSendData(ncb, lname, fname, OKMSG, OKSIZE, session);
  104.  
  105.     printf("\nContents of %s:\n\n", buffer + DXPFSIZE);
  106.     DispText(buffer + DXPFSIZE);
  107.   }
  108.   else
  109.     printf("Message not recognized.\n");
  110.  
  111.   NbCloseSession(ncb, lname, fname, session);
  112.   NbDelName(ncb, lname);
  113.   return 0;
  114. }
  115.  
  116. /**********************************
  117. * DispText -- Display a text file.
  118. **********************************/
  119.  
  120. void DispText (char *filename)
  121. {
  122.   FILE *fil;
  123.   char buffer[81];
  124.  
  125.   if(fil = fopen(filename, "rt"))
  126.   {
  127.     while(fgets(buffer, 80, fil))
  128.       printf("%s", buffer);
  129.     if(ferror(fil))
  130.       puts("\nError reading file.");
  131.  
  132.     fclose(fil);
  133.   }
  134. }
  135.  
  136. /*******************************************
  137. * DispHelp -- Display a brief help message.
  138. *******************************************/
  139.  
  140. static void DispHelp(void)
  141. {
  142.   printf("Usage: DXRECV <sender's name>\n");
  143.   printf("  Where <sender's name> is the network name ");
  144.   printf("sending the file\n");
  145.   printf("Note: The sender's name is case sensitive.\n");
  146. }
  147.  
  148.